package com.example.studentmanagersystem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.fs.adapters.ClassesAdapter;
import com.fs.utils.CmdUtils;

public class AdminAccountAddUserActivity extends Activity {
	
	private Spinner addUserSpinner;
	private Button submitBtn;
	private EditText addUserName,addUserPassword;
	private static final String m[] = {"学生", "老师" };
	private ArrayAdapter<String> addAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.admin_account_user_add_activity);
		//initialize 
		this.init();
	}
	
	
	/**
	 * initialize widget
	 */
	private void init() {
		this.addUserSpinner=(Spinner) this.findViewById(R.id.admin_addUser_sekect_activity);
		this.addUserName=(EditText) this.findViewById(R.id.admin_addUser_name_activity);
		this.addUserPassword=(EditText) this.findViewById(R.id.admin_addUser_password_activity);
		this.submitBtn=(Button) this.findViewById(R.id.admin_addUser_submitAddBtn_activity);
		this.addAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, m);
		this.addAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		this.addUserSpinner.setAdapter(addAdapter);//set adapter
		
		this.setSpinnerListener();//add user method
	}
	
	/**
	 * set Listener
	 */
	
	private void setSpinnerListener() {
		//select user identify
		this.addUserSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				
					addUserListener(arg2);//submit user info
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
		
	}
	
	/**
	 * addSubmit提交事件监听
	 */
	private void addUserListener(final int SELECT) {
		
				this.submitBtn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
						boolean isName=TextUtils.isEmpty(addUserName.getText());
						boolean isPsw=TextUtils.isEmpty(addUserPassword.getText());
						
						boolean isRight=(!isName)&&(!isPsw);
						
						if((SELECT==0)&&isRight){
							
							//invoking add student method 进行增加学生的操作
							
							showNormalDia(SELECT);
							
//							Toast.makeText(AdminAccountAddUserActivity.this, "您选择了学生，这里要进行添加学生的操作！", Toast.LENGTH_SHORT).show();
						
						}else if((SELECT==1)&&isRight){
							
							//invoking add teacher method 增加老师的操作
							
							showNormalDia(SELECT);
							
//							Toast.makeText(AdminAccountAddUserActivity.this, "您选择了老师，这里要进行添加老师的操作！", Toast.LENGTH_SHORT).show();
						}
					}
				});
	}
	
	
	/**
	 * add teacher method 管理员新增老师
	 */
	
	private void addminAddTec() {
		
		String name=addUserName.getText().toString().trim();
		String password=addUserPassword.getText().toString().trim();
		
		CmdUtils cmdUtils=new CmdUtils(this);
		
		String cmd="insert into tec(name,password)value('"+name+"','"+password+"')";
		
		cmdUtils.submitCmd(URL.CMD_URL, "add",cmd);
		 
	}
	
	

	/**
	 * add student method 管理员新增学生
	 */
	
	private void addminAddStu() {
		
			String name=addUserName.getText().toString().trim();
			String password=addUserPassword.getText().toString().trim();
			
			CmdUtils cmdUtils=new CmdUtils(this);
			
			String cmd="insert into stu(name,password)value('"+name+"','"+password+"')";
			 
			cmdUtils.submitCmd(URL.CMD_URL, "add",cmd);
		
		 
	}
	
	
	
	
	
	
	
	
	
	
	 /**
	   * show dialog 弹出提示对话框
	   */
	  private void showNormalDia(final int x){  
	        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext()); 
		  
		  boolean isName=TextUtils.isEmpty(addUserName.getText());
			boolean isPsw=TextUtils.isEmpty(addUserPassword.getText());
			
			if((!isName)&&(!isPsw)){
		  
	        AlertDialog.Builder normalDia=new AlertDialog.Builder(this);  
	        normalDia.setIcon(R.drawable.ic_launcher);  
	        normalDia.setTitle("提示");  
	        normalDia.setMessage("确认添加？");  
	          
	        normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	            	
	            	if(x==0){
	            		
	            		addminAddStu();
	            		
	            	}else if(x==1){
	            		
	            		addminAddTec();
	            	}
	            	
	            	showDia();
	                
	            }  
	        });  
	        
	        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	               
	            }  
	        });  
	        normalDia.create().show();  
	        
			}else if(isName||isPsw){
				
				AlertDialog.Builder normalDia=new AlertDialog.Builder(this);  
		        normalDia.setIcon(R.drawable.ic_launcher);  
		        normalDia.setTitle("提示");  
		        normalDia.setMessage("请正确输入！");  
		        normalDia.create().show(); 
			}
	    }  
	  
	  
	  /**
	   * second dialog 第二次对话框
	   */
	  private void showDia(){  
	        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());  
	        AlertDialog.Builder normalDia=new AlertDialog.Builder(this);  
	        normalDia.setIcon(R.drawable.ic_launcher);  
	        normalDia.setTitle("提示");  
	       
	        normalDia.setPositiveButton("继续添加？", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	            	
	            }  
	        });  
	        
	        normalDia.setNegativeButton("查看添加？", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	               
	            	AdminAccountAddUserActivity.this.finish();
	            }  
	        });  
	        normalDia.create().show();  
	    } 

	
	
}
